//
//  BCSwitchAccountCell.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/12/25.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "BCSwitchAccountCell.h"

#import "LocalAuthenticationManager.h"

@interface BCSwitchAccountCell ()

@property (nonatomic, strong) QMUIButton *contentButton;

//
@property (nonatomic, strong) UIImageView *headIV;
/**
 *accountLabel
 */
@property (nonatomic, strong) QMUILabel *accountLabel;

/**
 *authIV
 */
@property (nonatomic, strong) UIImageView *authIV;

@end


@implementation BCSwitchAccountCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupSubviews];
    }
    return self;
}
- (void)setHighlighted:(BOOL)highlighted {
    [super setHighlighted:highlighted];
    //
    self.contentButton.highlighted = highlighted;
}
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    [super setHighlighted:highlighted animated:animated];
    //
    self.contentButton.highlighted = highlighted;
}

- (void)setupSubviews {
    self.contentButton = [QMUIButton new];
    [self.contentView addSubview:self.contentButton];
    [self.contentButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.contentView);
    }];
    self.contentButton.userInteractionEnabled = false;
    //
    self.headIV = [UIImageView new];
    self.headIV.contentMode = UIViewContentModeScaleAspectFill;
    self.headIV.clipsToBounds = true;
    [self.contentButton addSubview:self.headIV];
    //
    CGFloat height = 40.0f;
    [self.headIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentButton);
        make.left.mas_equalTo(self.contentButton).offset(15.0);
        make.width.height.mas_equalTo(height);
    }];
    self.headIV.layer.cornerRadius = height/2;
    //
    self.authIV = [UIImageView new];
    self.authIV.contentMode = UIViewContentModeScaleAspectFit;
    self.authIV.tintColor = [UIColor darkMainColor];
    [self.contentButton addSubview:self.authIV];
    [self.authIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.contentButton).offset(-15.0);
        make.centerY.mas_equalTo(self.headIV);
        make.width.height.mas_equalTo(25.0);
    }];
    //
    self.accountLabel = [QMUILabel new];
    self.accountLabel.textColor = [UIColor blackColor];
    self.accountLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightRegular];
    [self.contentButton addSubview:self.accountLabel];
    self.accountLabel.adjustsFontSizeToFitWidth = true;
    [self.accountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.headIV.mas_right).offset(15.0);
        make.centerY.mas_equalTo(self.headIV);
    }];
    
    [self addSubview:self.separatorLine];
    [self.separatorLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(10.0);
        make.right.mas_equalTo(self.authIV.mas_left).offset(-10.0);
        make.bottom.mas_equalTo(self);
        make.height.mas_equalTo(1.0);
    }];
}

- (void)setUserInfo:(UserInfoModel *)userInfo {
    _userInfo = userInfo;
    //
    [self.headIV sd_setImageWithURL:[NSURL URLWithString:userInfo.head_img] placeholderImage:LOAD_IMAGE(@"default_head")];
    //
    NSString *account = userInfo.phone;
    if (!account.length) {
        account = userInfo.email;
    }
    self.accountLabel.text = account;
    
    //是否开启auth
    BOOL open = [[BCUserLoginManager sharedLogin] isOpenLocalAuthenticationWithUserId:userInfo.sub];
    if (open) {
        DDLABiometryType type = [LocalAuthenticationManager sharedLocalAuth].biometryType;
        switch (type) {
            case DDLABiometryTypeFaceID:
            {
                self.authIV.image =  [LOAD_IMAGE(@"bc_face") imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
            }
                break;
            case DDLABiometryTypeTouchID:
            {
                self.authIV.image =  [LOAD_IMAGE(@"bc_fingerprint") imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
            }
                break;
            default:
            {
                self.authIV.image = nil;
            }
                break;
        }
    }else {
        self.authIV.image = nil;
    }
}


+ (CGFloat)defaultHeight {
    return 50.0;
}

@end
